package com.qr.cch.it.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qr.cch.it.entity.Record;
import com.qr.cch.it.entity.Result;
import com.qr.cch.it.entity.StatusCode;
import com.qr.cch.it.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 描述:
 *
 * @author chenconghua
 * @version 1.0
 * @create 2022-10-01 11:46 PM
 */
@CrossOrigin
@RestController
@RequestMapping("/record")
public class RecordController {


    @Autowired
    private RecordService recordService;

    @PostMapping("add")
    public Result add(@RequestBody Map m) throws ParseException {


        String oaid=(String) m.get("oaid");
        String name=(String) m.get("name");
        String date1=(String) m.get("date1");
        String date2=(String) m.get("date2");
        String content=(String) m.get("content");
        String recontent=(String) m.get("recontent");
        String note=(String) m.get("note");

        Record record = new Record();
        record.setName(name);
        record.setContent(content);
        record.setOaid(oaid);


        String date3=date1.substring(0,10)+" "+date2.substring(11,19);
        String format = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(format);

        Date edate = sdf.parse(date3);
        record.setSetDate(edate);
        record.setCreateDate(new Date());
        record.setNote(note);
        record.setRecontent(recontent);


        if (recordService.save(record)){
            return new Result(true, StatusCode.OK,"添加成功");
        }else{
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }
    @PostMapping("selectBynote")
    public Result selectBynote(@RequestBody Map m) {


        String note=(String) m.get("note");


        QueryWrapper<Record> queryWrapper =new QueryWrapper<>();
        queryWrapper.like("note",note);

            return new Result(true, StatusCode.OK,"查询成功",recordService.list(queryWrapper));
    }
    @PostMapping("selectByname")
    public Result selectByname(@RequestBody Map m) {


        String name=(String) m.get("name");


        QueryWrapper<Record> queryWrapper =new QueryWrapper<>();
        queryWrapper.like("name",name);

        return new Result(true, StatusCode.OK,"查询成功",recordService.list(queryWrapper));
    }

    @PostMapping("selectByoaid")
    public Result selectByoaid(@RequestBody Map m) {


        String oaid=(String) m.get("oaid");


        QueryWrapper<Record> queryWrapper =new QueryWrapper<>();
        queryWrapper.like("oaid",oaid);

        return new Result(true, StatusCode.OK,"查询成功",recordService.list(queryWrapper));
    }

    @PostMapping("selectBycontent")
    public Result selectBycontent(@RequestBody Map m) {


        String content=(String) m.get("content");


        QueryWrapper<Record> queryWrapper =new QueryWrapper<>();
        queryWrapper.like("content",content);

        return new Result(true, StatusCode.OK,"查询成功",recordService.list(queryWrapper));
    }
    @GetMapping("list")
    public Result list(){



        List<Record> list =recordService.list();
        if (null!=list){
            return new Result(true,StatusCode.OK,"查询成功",list);
        }else{
            return new Result(false,StatusCode.ERROR,"查询失败");
        }
    }

    @PostMapping("selectByid")
    public Result selectByid(@RequestBody Map m) {


        Integer id=(Integer) m.get("id");
        Record record =recordService.getById(id);
        if (null!=record){
            return new Result(true, StatusCode.OK,"查询成功",record);
        }else {
            return new Result(false, StatusCode.ERROR,"查询失败");
        }


//        QueryWrapper<Record> queryWrapper =new QueryWrapper<>();
//        queryWrapper.like("note",note);


    }
}
